
#include "NadineFramework0316/NadineFramework/NadineLibrary/Nadine09.h"
#include "NDLoadingScreen.h"

namespace Nadine
{
	//---------------------------------------------------------------------
	void NDLoadingScreen::animateLoadingScreen( float fElapsedTime )
	{
		float aniTime =fElapsedTime;
		if (aniTime >0.3f)
			aniTime =0.3f;

		bool bCheckState =true;
		if (fabs(m_fTargetHeight -m_fCurrentHeight) >0.5f)
		{
			m_fCurrentHeight +=(m_fTargetHeight -m_fCurrentHeight)*0.5f*aniTime*15.0f;
			bCheckState =false;
		}else
			m_fCurrentHeight =m_fTargetHeight;


		if (fabs(m_fTargetPosition -m_fCurrentPosition) >0.5f)
		{
			m_fCurrentPosition +=(m_fTargetPosition -m_fCurrentPosition)*0.5f*aniTime*15.0f;
			bCheckState =false;
		}else
			m_fCurrentPosition =m_fTargetPosition;

		if (m_nState ==2)
		{
			for (int i=0;i<CIRCLES_NUM;++i)
			{
				if (fabs(m_fCircleTargetWidth -m_fCircleWidth[i]) >0.5f)
				{
					m_fCircleWidth[i] +=(m_fCircleTargetWidth -m_fCircleWidth[i])*0.25f*aniTime*5.0f;
				}else
				{
					m_fCircleWidth[i] =m_fCircleTargetWidth;
				}
			}

			static float ct =0;
			ct+=aniTime;
			if (ct >=0.1f)
			{
				m_nCurrentCircle++;
				if (m_nCurrentCircle==CIRCLES_NUM)
					m_nCurrentCircle =0;
				m_fCircleWidth[m_nCurrentCircle] =15.0f;

				ct =0;
			}
		}

		if (bCheckState)
		{
			if (m_nState ==1)
				m_nState =2;

			if (m_nState ==3)
				m_nState =0;
		}

	}

	//---------------------------------------------------------------------
	NDLoadingScreen::NDLoadingScreen( NDArk* pArk ) :NDBaseObject(99)
	{
		this->setArk( pArk );

		m_nState =0;
		m_fTargetHeight =0.0f;
		m_fCurrentHeight =0.0f;

		m_fCurrentPosition =0.0f;
		m_fTargetPosition =0.0f;

		m_fCircleTargetWidth =10.0f;

		m_nCurrentCircle =0;
		for (int i=0;i<CIRCLES_NUM;++i)
			m_fCircleWidth[i] =10.0f;

	}

	//---------------------------------------------------------------------
	void NDLoadingScreen::release()
	{
		this->m_bReleased =true;


	}

	//---------------------------------------------------------------------
	void NDLoadingScreen::initialize()
	{
		this->m_nBGTex0 =NDTEXMGR->addTextureFromFile(L"../Resources/GUITextures/whitebg.tif", false);
		this->m_nBGTex1 =NDTEXMGR->addTextureFromFile(L"../Resources/GUITextures/circle.tif", true);

		this->m_fCurrentPosition =NDW32OS->getWindowHeight()*0.5f;


	}

	//---------------------------------------------------------------------
	void NDLoadingScreen::render(float fElapsedTime)
	{
		animateLoadingScreen(fElapsedTime);

		//
		if (m_nState ==0)
			return;

		float fW =NDW32OS->getWindowWidth();
		float fH =NDW32OS->getWindowHeight();

		//BG
		glColor4f(0.0f, 0.25f, 0.5f, 0.9f);
		NDTEXMGR->drawTexture( m_nBGTex0, 0.0f, m_fCurrentPosition, m_fCurrentHeight, fW );

		if (m_nState ==2)
		{
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			for (int j=0;j<4;++j)
			{
				for (int i=0;i<2;++i)
				{
					NDTEXMGR->drawTexture( m_nBGTex1, 
						fW*0.5f-(25.0f*2.0f)+((float)j*25.0f)+(25.0f*0.5f)-(m_fCircleWidth[i*4+j]*0.5f), 
						fH*0.5f-(25.0f*0.5f)+((float)i*25.0f)-(m_fCircleWidth[i*4+j]*0.5f), 
						m_fCircleWidth[i*4+j],
						m_fCircleWidth[i*4+j] );

				}

			}
		}

		//...


	}

	//---------------------------------------------------------------------
	void NDLoadingScreen::beginLoading()
	{
		m_nState =1;

		this->m_fTargetHeight = 150.0f;
		this->m_fTargetPosition =NDW32OS->getWindowHeight()*0.5f -(m_fTargetHeight*0.5f);
	}

	//---------------------------------------------------------------------
	void NDLoadingScreen::endLoading()
	{
		m_nState =3;

		this->m_fTargetHeight = 0.0f;
		this->m_fTargetPosition =NDW32OS->getWindowHeight()*0.5f;
	}

	//---------------------------------------------------------------------
	void NDLoadingScreen::setWindowSz( UINT nW, UINT nH )
	{
	}

}